package com.ruoyi.web.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpClientUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.fs.domain.FsLawyer;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Anonymous
@RestController
@RequestMapping("/guide")
public class GuideController extends BaseController {

    @Autowired
    private ISysUserService userService;

    /**
     * 获取证据规则
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(
    ) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        user = userService.selectUserById(user.getUserId());
        String url = "https://tj.angchengls.com/osapi/Login/Loginact";
        String username = user.getZwsAccount();
        String password = user.getZwsPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return error("当前账户尚未配置调解系统账号！");
        }
        Map<String, String> param = new HashMap<>();
        param.put("username", username);
        param.put("password", password);

        String result = HttpClientUtils.doPost(url, param);
        JSONObject obj = JSONObject.parseObject(result);
        int code = obj.getInteger("code");
        if (code == 0) {
            return error(obj.getString("msg"));
        }
        JSONObject data = obj.getJSONObject("data");
        Map<String, String> map = new HashMap<>();
        map.put("token", data.getString("token"));
        map.put("id", data.getJSONObject("memberInfo").getString("id"));
        return success(map);
    }
}
